package com.liefox.controller;

import com.liefox.pojo.Patient;
import com.liefox.pojo.User;
import com.liefox.service.PatientService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;


@Controller
public class PatientController {

    @Autowired
    private PatientService patientService;
    @Autowired
    JavaMailSenderImpl mailSender;

    /*获取全部病者信息*/
    @RequestMapping("/getPatientInfo")
    public String getPatientInfo(Model model) {
        List<Patient> patientInfo = patientService.getPatientInfo();
        model.addAttribute("patientInfos", patientInfo);
        return "doctor/app/table-editable";
    }

    /*删除病人信息根据UUID*/
    @RequestMapping("/delPatientInfoByUUID/{UUID}")
    public String delPatientInfoByUUID(@PathVariable("UUID") String UUID) {
        patientService.delPatientInfoByUUID(UUID);
        return "redirect:/getPatientInfo";
    }

    /*获取病人信息根据UUID*/
    @RequestMapping("/getPatientInfoByUUID/{UUID}")
    public String getPatientInfoByUUID(@PathVariable("UUID") String UUID, Model model) {
        Patient patientInfoByUUID = patientService.getPatientInfoByUUID(UUID);
        System.out.println(patientInfoByUUID);
        model.addAttribute("patientInfoByUUID", patientInfoByUUID);
        return "doctor/app/rePatientInfo";
    }

    /*更新病人信息根据UUID*/
    @RequestMapping("/upPatientInfoByUUID")
    public String upPatientInfoByEmail(Patient patient) {
        patientService.upPatientInfoByUUID(patient);
        return "redirect:/getPatientInfo";
    }

    /*去新增病患页*/
    @RequestMapping("/toAddPatientInfo")
    public String toAddPatientInfo(Model model) {
        String uuid = UUID.randomUUID().toString();
        model.addAttribute("uuid", uuid);
        return "doctor/app/addPatientInfo";
    }


    /*新增病患*/
    @RequestMapping("/addPatientInfo")
    public String addPatientInfo(Patient patient, Model model) {
        int i = patientService.addPatientInfo(patient);
        model.addAttribute("msg", "添加成功！");
        return "redirect:/getPatientInfo";
    }

    /*去发邮件页面*/
    @RequestMapping("/toEmail/{Email}")
    public String toEmail(@PathVariable("Email") String Email, Model model) {
        model.addAttribute("PatientEmail", Email);
        return "doctor/app/email-compose";
    }

    /*发邮件给病者*/
    @RequestMapping("/sentEmail")
    public String recPwEmail(String ToEmail, String CcEmail, String subject, String Message,
                             HttpSession session, Model model) {
        try {
            //邮件设置
            SimpleMailMessage message = new SimpleMailMessage();
            //主题
            message.setSubject(subject);
            //内容
            message.setText(Message);
            //收件人
            message.setTo(ToEmail);
            //发件人
            message.setFrom(CcEmail);
            //发送
            mailSender.send(message);
            model.addAttribute("info", "邮件发送成功！");
            return "doctor/app/email-compose";
        } catch (Exception e) {
            model.addAttribute("info", "邮箱地址不正确！");
            return "doctor/app/email-compose";
        }


    }

}
